package com.jqsoft.launcher.util;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.json.JSONObject;

import java.util.Iterator;

public class XmlUtil {
	
	//解析蓝牙设备返回的XML字符串
	public static String parseXml(String xml){
		JSONObject jsonObject = new JSONObject();
		try {
			Document doc = DocumentHelper.parseText(xml);
			Element rootElt = doc.getRootElement();
			Iterator urdataIte = rootElt.elementIterator("URData");
			while (urdataIte.hasNext()) {
				Element urdataEle = (Element) urdataIte.next();
				String type = urdataEle.attributeValue("Type");
				if ("C".equals(type)) {//监护数据
					Iterator resultsIte = urdataEle.elementIterator("Results");
					while (resultsIte.hasNext()) {
						Element resultsEle = (Element) resultsIte.next();
						Iterator resultIte = resultsEle.elementIterator("Result");
						while (resultIte.hasNext()) {
							Element resultEle = (Element) resultIte.next();
							String item = resultEle.attributeValue("Item");
							if ("HeartRate".equals(item)) {//心率
								Element value = resultEle.element("Value");
								jsonObject.put("cHeartRate", value.getText());
							}
							if ("BreathRate".equals(item)) {//呼吸率
								Element value = resultEle.element("Value");
								jsonObject.put("cBreathRate", value.getText());
							}
							if ("SpO2".equals(item)) {//血氧
								Element value = resultEle.element("Value");
								jsonObject.put("cSpO2", value.getText());
							}
							if ("PulseRate".equals(item)) {//脉率
								Element value = resultEle.element("Value");
								jsonObject.put("cPulseRate", value.getText());
							}
							if ("SP".equals(item)) {//收缩压
								Element value = resultEle.element("Value");
								jsonObject.put("cSP", value.getText());
							}
							if ("DP".equals(item)) {//舒张压
								Element value = resultEle.element("Value");
								jsonObject.put("cDP", value.getText());
							}
							if ("MAP".equals(item)) {//平均压
								Element value = resultEle.element("Value");
								jsonObject.put("cMAP", value.getText());
							}
							if ("Temp".equals(item)) {//体温
								Element value = resultEle.element("Value");
								jsonObject.put("cTemp", value.getText());
							}
						}
					}
				}
				if ("G".equals(type)) {//血糖分析数据
					Iterator resultsIte = urdataEle.elementIterator("Results");
					while (resultsIte.hasNext()) {
						Element resultsEle = (Element) resultsIte.next();
						Iterator resultIte = resultsEle.elementIterator("Result");
						while (resultIte.hasNext()) {
							Element resultEle = (Element) resultIte.next();
							String item = resultEle.attributeValue("Item");
							if ("GLU".equals(item)) {//血糖值
								Element value = resultEle.element("Value");
								jsonObject.put("gGLU", value.getText());
							}
						}
					}
				}
				if ("H".equals(type)) {//血红蛋白分析数据
					Iterator resultsIte = urdataEle.elementIterator("Results");
					while (resultsIte.hasNext()) {
						Element resultsEle = (Element) resultsIte.next();
						Iterator resultIte = resultsEle.elementIterator("Result");
						while (resultIte.hasNext()) {
							Element resultEle = (Element) resultIte.next();
							String item = resultEle.attributeValue("Item");
							if ("Hb".equals(item)) {//血红蛋白值
								Element value = resultEle.element("Value");
								jsonObject.put("hHb", value.getText());
							}
						}
					}
				}
				if ("U".equals(type)) {//尿液分析数据
					Iterator resultsIte = urdataEle.elementIterator("Results");
					while (resultsIte.hasNext()) {
						Element resultsEle = (Element) resultsIte.next();
						Iterator resultIte = resultsEle.elementIterator("Result");
						while (resultIte.hasNext()) {
							Element resultEle = (Element) resultIte.next();
							String item = resultEle.attributeValue("Item");
							if ("WBC".equals(item)) {//白细胞
								Element value = resultEle.element("Value");
								jsonObject.put("uWBC", value.getText());
							}
							if ("KET".equals(item)) {//酮体
								Element value = resultEle.element("Value");
								jsonObject.put("uKET", value.getText());
							}
							if ("NIT".equals(item)) {//亚硝酸盐
								Element value = resultEle.element("Value");
								jsonObject.put("uNIT", value.getText());
							}
							if ("URO".equals(item)) {//尿胆原
								Element value = resultEle.element("Value");
								jsonObject.put("uURO", value.getText());
							}
							if ("BIL".equals(item)) {//胆红素
								Element value = resultEle.element("Value");
								jsonObject.put("uBIL", value.getText());
							}
							if ("PRO".equals(item)) {//尿蛋白
								Element value = resultEle.element("Value");
								jsonObject.put("uPRO", value.getText());
							}
							if ("GLU".equals(item)) {//葡萄糖
								Element value = resultEle.element("Value");
								jsonObject.put("uGLU", value.getText());
							}
							if ("SG".equals(item)) {//尿比重
								Element value = resultEle.element("Value");
								jsonObject.put("uSG", value.getText());
							}
							if ("BLD".equals(item)) {//隐血+
								Element value = resultEle.element("Value");
								jsonObject.put("uBLD", value.getText());
							}
							if ("pH".equals(item)) {//酸碱度
								Element value = resultEle.element("Value");
								jsonObject.put("upH", value.getText());
							}
							if ("Vc".equals(item)) {//维C
								Element value = resultEle.element("Value");
								jsonObject.put("uVc", value.getText());
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

}
